﻿using ExpressesClass.KyApi.Base;
using ExpressesClass.KyApi.billApply.request;
using ExpressesClass.KyApi.billApply.response;
using ExpressesClass.KyApi.billCancel.request;
using ExpressesClass.KyApi.billCancel.response;
using ExpressesClass.KyApi.billTrace.request;
using ExpressesClass.KyApi.billTrace.response;
using ExpressesClass.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ExpressesClass.IExpresses
{
    public class KyExpress : IExpress
    {
        //跨越快递参数
        private static string appKey = "";
        private static string appSecret = "";
        private static string customerCode = "";
        private static string platformFlag = "";
        private static string paymentCustomer = "";

        //测试
        //private static string appKey = "";
        //private static string appSecret = "";
        //private static string customerCode = "";
        //private static string platformFlag = "";
        //private static string paymentCustomer = "";

        public Kddh GetMailNo(Kddh kddh)
        {
            BillApplyReq billApplyReq = new BillApplyReq();
            billApplyReq.customerCode = customerCode;
            billApplyReq.platformFlag = platformFlag;

            OrderInfo orderInfo = new OrderInfo();

            orderInfo.preWaybillDelivery.companyName = kddh.sendCompany;
            orderInfo.preWaybillDelivery.person = kddh.sendMan;
            orderInfo.preWaybillDelivery.mobile = kddh.sendManPhone;
            orderInfo.preWaybillDelivery.provinceName = kddh.sendProvince;
            orderInfo.preWaybillDelivery.cityName = kddh.sendCity;
            orderInfo.preWaybillDelivery.countyName = kddh.sendCounty;
            orderInfo.preWaybillDelivery.address = kddh.sendManAddress;

            orderInfo.preWaybillPickup.companyName = kddh.receiveCompany;
            orderInfo.preWaybillPickup.person = kddh.receiveMan;
            orderInfo.preWaybillPickup.mobile = kddh.receiveManPhone;
            orderInfo.preWaybillPickup.provinceName = kddh.receiveProvince;
            orderInfo.preWaybillPickup.cityName = kddh.receiveCity;
            orderInfo.preWaybillPickup.countyName = kddh.receiveCounty;
            orderInfo.preWaybillPickup.address = kddh.receiveManAddress;

            orderInfo.serviceMode = 40;
            orderInfo.payMode = 10;
            orderInfo.paymentCustomer = paymentCustomer;
            orderInfo.goodsType = String.IsNullOrEmpty(kddh.itemName) ? "物品" : kddh.itemName;
            orderInfo.count = int.Parse(kddh.itemCount);

            orderInfo.orderId = kddh.orderId;
            orderInfo.insuranceValue = decimal.Parse(kddh.insuranceValue);
            orderInfo.receiptFlag = 20;
            orderInfo.waybillRemark = kddh.remark;
            orderInfo.dismantling = 10;
            orderInfo.goodsTime = DateTime.Now.AddMinutes(10).ToString("yyyy-MM-dd HH:mm");

            billApplyReq.orderInfos.Add(orderInfo);

            KyClient kyClient = new KyClient(appKey, appSecret);

            BillApplyRsp res = kyClient.execute<BillApplyRsp>(billApplyReq);

            if (res.success)
            {
                Kddh k = new Kddh();
                k.orderId = res.data[0].orderId;
                k.mailNo = res.data[0].waybillNumber;
                k.mailno_zd = String.Join(",", res.data[0].subpartIdentification);
                k.sendMan = kddh.sendMan;
                k.sendManPhone = kddh.sendManPhone;
                k.sendManAddress = kddh.sendManAddress;
                k.sendPostcode = kddh.sendPostcode;
                k.sendProvince = kddh.sendProvince;
                k.sendCity = kddh.sendCity;
                k.sendCounty = kddh.sendCounty;
                k.sendCompany = kddh.sendCompany;
                k.receiveMan = kddh.receiveMan;
                k.receiveManPhone = kddh.receiveManPhone;
                k.receiveManAddress = kddh.receiveManAddress;
                k.receivePostcode = kddh.receivePostcode;
                k.receiveProvince = kddh.receiveProvince;
                k.receiveCity = kddh.receiveCity;
                k.receiveCounty = kddh.receiveCounty;
                k.receiveCompany = kddh.receiveCompany;
                k.sortingCode = "";
                k.pkgCode = "";
                k.shipName = kddh.shipName;
                k.shipVal = kddh.shipVal;
                k.itemName = kddh.itemName;
                k.itemWeight = kddh.itemWeight;
                k.itemCount = kddh.itemCount;
                k.remark = kddh.remark;
                k.bm = kddh.bm;
                k.wzh = kddh.wzh;
                k.insuranceValue = kddh.insuranceValue;
                k.kdCompanys = kddh.kdCompanys;
                k.scenarios = kddh.scenarios;
                k.printNum = kddh.printNum;
                k.payMethod = kddh.payMethod;
                k.isReturnNo = kddh.isReturnNo;
                return k;
            }
            else if (!res.success && !string.IsNullOrEmpty(res.msg))
            {
                MessageBox.Show(res.msg, "提示");
            }
            else
            {
                MessageBox.Show("获取快递电子面单失败.", "提示");
            }
            return null;
        }

        public bool CancelMailNo(Kddh kddh)
        {
            BillCancelReq billCancelReq = new BillCancelReq();

            billCancelReq.customerCode = customerCode;
            billCancelReq.waybillNumber = kddh.mailNo;

            KyClient kyClient = new KyClient(appKey, appSecret);

            BillCancelRsp res = kyClient.execute<BillCancelRsp>(billCancelReq);

            if (res.success)
            {
                return true;
            }
            else if (!res.success && !string.IsNullOrEmpty(res.msg))
            {
                MessageBox.Show("面单号:" + kddh.mailNo + " ," + res.msg, "提示");
            }
            else
            {
                MessageBox.Show("面单号:" + kddh.mailNo + " ,取消快递电子面单失败,请重试.", "提示");
            }
            return false;
        }

        public List<TraceLog> GetExpressMsg(Kddh kddh)
        {
            BillTraceReq billTraceReq = new BillTraceReq();
            billTraceReq.waybillNumbers.Add(kddh.mailNo);

            KyClient kyClient = new KyClient(appKey, appSecret);

            BillTraceRsp res = kyClient.execute<BillTraceRsp>(billTraceReq);

            if (res.success)
            {
                List<TraceLog> log = new List<TraceLog>();

                if (res.data != null && res.data.esWaybill != null && res.data.esWaybill.Count > 0 && res.data.esWaybill[0].exteriorRouteList != null && res.data.esWaybill[0].exteriorRouteList.Count > 0)
                {
                    foreach (ExteriorRoute r in res.data.esWaybill[0].exteriorRouteList)
                    {
                        TraceLog t = new TraceLog();
                        t.acceptAddress = r.city;
                        t.acceptTime = r.uploadDate;
                        t.scanType = r.routeStep;
                        t.remark = r.routeDescription;
                        log.Add(t);
                    }
                }

                return log;
            }
            else if (!res.success && !string.IsNullOrEmpty(res.msg))
            {
                MessageBox.Show(res.msg, "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
            return null;
        }

        public void GetUseNum()
        {
            MessageBox.Show("跨越快递是月结形式.", "提示");
        }
    }
}
